import { axios } from '@/utils/request'
import store from '@/store'
import storage from 'store'
import { ACCESS_TOKEN } from '@/store/mutation-types'

const publicConfig = store.getters.publicConfig

const api = {
  list: '/user.application/list',
  add: '/user.application/add',
  edit: '/user.application/edit',
  batchEdit: '/user.application/batchEdit',
  delete: '/user.application/delete',
  exportAvatar: '/user.application/exportAvatar'
}

/**
 * 列表
 */
export function list (params) {
  return axios({
    url: api.list,
    method: 'get',
    params
  })
}

/**
 * 导出学员
 */
export function exportExcel (params) {
  const token = storage.get(ACCESS_TOKEN)
  params['Access-Token'] = token
  var newParams = []
  for (const k in params) {
    if (params[k] != undefined) {
      newParams.push(`${k}=${params[k]}`)
    }
  }
  window.location.href = publicConfig.BASE_API + api.list + '&' + newParams.join('&')
}

/**
 * 导出头像
 */
export function exportAvatar (data) {
  return axios({
    url: api.exportAvatar,
    method: 'post',
    data: data
  })
}

/**
 * 新增记录
 * @param {*} data
 */
export function add (data) {
  return axios({
    url: api.add,
    method: 'post',
    data
  })
}

/**
 * 编辑记录
 * @param {*} data
 */
export function edit (data) {
  return axios({
    url: api.edit,
    method: 'post',
    data
  })
}

/**
 * 批量编辑
 * @param {*} data
 */
export function batchEdit (data) {
  return axios({
    url: api.batchEdit,
    method: 'post',
    data
  })
}

/**
 * 删除记录
 * @param {*} data
 */
export function deleted (data) {
  return axios({
    url: api.delete,
    method: 'post',
    data: data
  })
}
